import { article as articleModel, type as articleTypeModel} from '../models/Article'
import chalk from 'chalk'

class Article {

    constructor() {
        this.articleParams = []
        this.articleParamsError = ['标题', '描述', 'url', '排序值', '类型', '内容', '是否可见', '预览内容', '预览图']
        this.addArticle = this.addArticle.bind(this)
    } //构造函数

    async addArticle({body}, res, next){ //添加文章

        const flag1 = this.checkArticleParams(body,res) //校验前台参数值

        const flag2 = this.checkArticleExist(body.url,res) //检查是否有相同文章存在

        if(flag1 && flag2){ //如果通过前台校验

            console.log('Successful')

            let { title, description, url, sort, type, content, hidden, preContent, preImg } = body

            const Article = {
                title, 
                description, 
                url, 
                sort, 
                type, 
                content, 
                hidden, 
                preContent, 
                preImg
            }
            try{
                const articlemodel = new articleModel(Article)
                await articlemodel.save()
                res.send({
                    status: 1,
                    type: '添加餐馆成功',
                    message: Article
                })
            }catch(err){
                console.log(chalk.red(`博客文章保存失败`))
                res.send({
                    status: 0,
                    type: 'ERROR_SERVER',
                    message: '博客文章保存失败'
                })
            }
            
        }
        
    }

    checkArticleParams(body, res){ //校验前台参数值

        this.articleParams = [] //初始化
        
        let { title, description, url, sort, type, content, hidden, preContent, preImg } = body

        this.articleParams.push(title, description, url, sort, type, content, hidden, preContent, preImg)

        try {
            this.articleParams.map((value,index)=>{
                if (!value)
                    throw new Error(`必须填写文章${this.articleParamsError[index]}`)
            })

        } catch (err) {
            console.log(chalk.red(`前台文章参数错误,${err.message}`))
            res.send({
                status: 0,
                type: 'ERROR_PARAMS',
                message: err.message
            })
            return false
        }
        return true
    }

    async checkArticleExist(url, res){ //判断是否有url相同的文章存在
        if(!url)
            return false
        try{
            const urlObj = {url: url}

            let ans = await articleModel.findOne(urlObj)

            if (ans) { //存在相同的url的文章
                res.send({
                    status: 0,
                    type: '存在url相同的文章',
                    message: '添加失败,已存在url相同的文章'
                })
                return false
            }   
                
        }catch(err){
            console.log(chalk.red(`查询相同文章失败`))
            res.send({
                status: 0,
                type: 'ERROR_SERVER',
                message: err.message
            })
            return false
        }
        return true
    }

}

export default new Article()